<extend name="Layout:layout_dialog" />
<block name="body_main">
    <form action="{:U('invoicing_in_edit_save')}" method="post" class="xsui-form" style="min-width:800px;">
        <fieldset>
            <div class="xsui-form-field">
                <label>入库日期：</label>
                <input type="text" name="time_date" class="datepicker" size="10" placeholder="入库日期" required="required" value="{:date('Y-m-d')}" />
            </div>
            <div class="xsui-form-field">
                <label>入库类型：</label>
                <select name="operate_type">{:sofn_combobox_option(array('PURCHASE'=>'采购入库','SELL-RETURN'=>'退货入库','INVENTORY'=>'盘点调仓'),$data['operate_type'],'PURCHASE')}</select>
            </div>
            <div class="xsui-form-field">
                <label>选择商品：</label>
                <select name="product_id" data-url="{:U('ProductGoods/get_goods_json_list')}" data-item-url="{:U('ProductGoods/get_goods_json_item')}" data-value="{$data.product_id}" required="required"></select>
            </div>
            <div class="xsui-form-field">
                <label>商品数量：</label>
                <input type="text" name="number" value="{$data.number}"  placeholder="商品数量" required="required" />
            </div>
            <div class="xsui-form-field">
                <label>商品价格：</label>
                <input type="text" name="price" value="{$data.price}" placeholder="商品价格" required="required" />
            </div>
            <div class="xsui-form-field">
                <label>入库备注：</label>
                <input type="text" name="remark" value="{$data.remark}" placeholder="入库备注" />
            </div>
            <div class="xsui-form-field">
                <label>小计金额：</label>
                <span id="subtotal">0.00</span>
            </div>
            <div class="xsui-form-field">
                <label>原有库存：</label>
                <span id="old_stock">{$data.old_stock}</span>
                <input type="hidden" name="old_stock" value="{$data.old_stock}" />
            </div>
            <div class="xsui-form-field">
                <label>最新库存：</label>
                <span id="new_stock">0.00</span>
            </div>
        </fieldset>
        <button type="submit" class="ui-state-default"><i class="fa fa-save"></i>发布</button>
        <button type="reset" class="ui-state-default">重置</button>
        <input type="hidden" name="site_id" value="{$data.site_id}"/>
        <input type="hidden" name="store_id" value="{$data.store_id}"/>
        <input type="hidden" name="user_name" value="{$data.user_name}"/>
        <input type="hidden" name="direction_type" value="I"/>
    </form>
</block>
<block name="body_after">
    <script>
        $(function () {
            ajax_combobox({selector: 'select[name="product_id"]', def_option: '请选择商品', func: function () {
                    product_change();
                }});
            $('select[name="product_id"]').change(function () {
                product_change();
            })
            // 自动计算 2016-5-30
            $('[name="number"],[name="price"]').blur(function () {
                calc();
            })
        })
        // 2016-6-1 sochishun added.
        function calc() {
            var aitem = {
                'number': parseInt($('[name="number"]').val()),
                'price': parseInt($('[name="price"]').val()),
            };
            if (!aitem.number || !aitem.price) {
                return;
            }
            $('#subtotal').text(aitem.number * aitem.price);
            $('#new_stock').text(parseInt($('#old_stock').text()) + aitem.number);
        }
        function product_change(id) {
            var $el = $('select[name="product_id"]');
            if (!id) {
                id = $el.val();
            }
            if (!id) {
                id = $el.data('value');
            }
            if (!id) {
                return;
            }
            $.get($el.data('itemUrl'), {id: id}, function (data) {
                $('#old_stock').text(data.stock);
                $('[name="old_stock"]').val(data.stock);
                $('[name="price"]').val(data.price);
                calc();
            })
        }
    </script>
</block>